#include "ebookpage.h"

using namespace std;

EBookPage::~EBookPage()
{
}

EBookPage::EBookPage(const EBookPage& other)
{
    number = other.number;
    data = other.data;
}

bool EBookPage::addLine(const string& line)
{
    data.append(line);
    data.append("\n");
    return true;
}

bool EBookPage::containsWord(string word)
{
    std::string dataLower = data;
    std::transform(word.begin(), word.end(), word.begin(), ::tolower);
    std::transform(dataLower.begin(), dataLower.end(), dataLower.begin(), ::tolower);
    int index = dataLower.rfind(word);

    if (index != -1)
    {
        unsigned int wordLength = word.length();
        char next = (wordLength + index >= dataLower.size()) ? ' ' : dataLower.at(index + wordLength);
        char previous = (index == 0) ? ' ' : dataLower.at(index - 1);

        bool wordBounded = !(isalpha(next) || isalpha(previous));
        return wordBounded;
    }

    return false;
}

string EBookPage::getContent()
{
    return data;
}
